/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.autosalon.ui.objednavka;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.model.Adresa;
import com.autosalon.model.Automobil;
import com.autosalon.model.Banka;
import com.autosalon.model.Doplnek;
import com.autosalon.model.Financovani;
import com.autosalon.model.Poisteni;
import com.autosalon.model.Poistovna;
import com.autosalon.model.Pujcka;
import com.autosalon.model.Role;
import com.autosalon.model.StavFinancovani;
import com.autosalon.model.StavObjednavky;
import com.autosalon.model.Zakaznik;
import com.autosalon.tools.Constants;
import com.autosalon.tools.IPaging;
import com.autosalon.tools.PagingComponent;
import com.autosalon.ui.ContentSwitcher;
import com.autosalon.ui.objednavka.action.BackObjednavkaAction;
import com.autosalon.ui.objednavka.action.CancelObjednavkaAction;
import com.autosalon.ui.objednavka.action.CreateObjednavkaAction;
import com.autosalon.ui.objednavka.action.NewObjednavkaAction;
import com.autosalon.ui.objednavka.action.PriradObjednavkaAction;
import com.autosalon.ui.objednavka.action.SaveObjednavkaAction;
import com.autosalon.ui.objednavka.action.ShowObjednavkaAction;
import com.autosalon.ui.objednavka.state.ObjednavkaStateHolder;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.VerticalLayout;

/**
 *
 * @author s0vet
 */
public class Objednavka extends HorizontalLayout implements IPaging {
    
    private final ContentSwitcher switcher;
    
    private Form newObjednavkaForm;
        private Table tabulkaAutomobilu;
        private TextField tfNewObjVyrobce;
        private TextField tfNewObjModel;
        private TextField tfNewObjCena;
        private TextField tfNewObjNajeto;
        private PopupDateField pdNewObjDatum;
        private TwinColSelect tsNewObjVybava;
    
    private Form createObjednavkaForm;
        private ComboBox cbCreateObjZakaznik;
        private TextField tfCreateObjJmeno;
        private TextField tfCreateObjPrijmeni;
        private ComboBox cbCreateObjAdresa;
        private TextField tfCreateObjKontakt;
        private TextField tfCreateObjVyrobce;
        private TextField tfCreateObjModel;
        private PopupDateField pdCreateObjDatum;
        private TextField tfCreateObjNajeto;
        private TextField tfCreateObjCena;
        private ComboBox cbCreateObjDoplnky;
        private TextArea taCreateObjDescription;
        private ComboBox cbCreateObjPojistovna;
        private Label lblDescription;
        private DateField dfCreateObjPojisteniFrom;
        private DateField dfCreateObjPojisteniTo;
        private HorizontalLayout hlBanka;
        private ComboBox cbCreateObjFinancovani;
        private ComboBox cbCreateObjBanka;
        private HorizontalLayout hlFinancovaniFromTo;
        private DateField dfCreateObjFinancovaniFrom;
        private DateField dfCreateObjFinancovaniTo;
        
    private Form showObjednavkaForm;
        private com.autosalon.model.Objednavka showedObjednavka;
        private Label lblShowObjJmenoPrijmeni;
        private Label lblShowObjEmail;
        private Label lblShowObjTelefon;
        private Label lblShowObjFinancovani;
        private Label lblShowObjBanka;
        private Label lblShowObjFinancovaniFrom;
        private Label lblShowObjFinancovaniTo;
        private Label lblShowObjVyrobce;
        private Label lblShowObjModel;
        private Label lblShowObjDatum;
        private Label lblShowObjNajeto;
        private Label lblShowObjCena;
        private Label lblShowObjDoplnky;
        private Label lblShowObjPojistovna;
        private Label lblShowObjPojistovnaDescription;
        private Label lblShowObjPojisteniFrom;
        private Label lblShowObjPojisteniTo;
        private Label lblShowObjDescription;
        private ComboBox cbShowObjStav;
        
    private final Calendar cal;
    private HorizontalLayout hlNewCreateComboZakaznik;
    private VerticalLayout vlObjednavka;    
        private PagingComponent pagingObjComponent;
        private Table tabulkaObjednavek;
        
    
    private Automobil actuallySelectedCar;
    
    public Objednavka(ContentSwitcher switcher) {
        this.switcher = switcher;
        cal = Calendar.getInstance();
        init();
    }
    
    public void init() {
        vlObjednavka = new VerticalLayout();
        vlObjednavka.setSpacing(true);
        vlObjednavka.setMargin(true);
        vlObjednavka.setCaption("Seznam objednávek");
        
        pagingObjComponent = new PagingComponent(this);
        vlObjednavka.addComponent(pagingObjComponent);
        
        tabulkaObjednavek = new Table();
        tabulkaObjednavek.setCaption("Objednávky");
        tabulkaObjednavek.setSizeFull();
        tabulkaObjednavek.setSelectable(false);
        tabulkaObjednavek.setWidth("100%");
        tabulkaObjednavek.setHeight("100%");
        tabulkaObjednavek.addContainerProperty("ZÁKAZNÍK", String.class, null);
            tabulkaObjednavek.setColumnWidth("ZÁKAZNÍK", 130);
        tabulkaObjednavek.addContainerProperty("AUTOMOBIL", String.class, null);    
            tabulkaObjednavek.setColumnWidth("AUTOMOBIL", 150);
        tabulkaObjednavek.addContainerProperty("DATUM", Date.class, null);    
            tabulkaObjednavek.setColumnWidth("DATUM", 85);
        tabulkaObjednavek.addContainerProperty("PRODEJCE", String.class, null);    
            tabulkaObjednavek.setColumnWidth("PRODEJCE", 130);
        tabulkaObjednavek.addContainerProperty("STAV", String.class, null);    
            tabulkaObjednavek.setColumnWidth("STAV", 100);
        tabulkaObjednavek.addContainerProperty("Ukaž", Button.class, null);
            tabulkaObjednavek.setColumnWidth("Ukaž", 35);
        tabulkaObjednavek.addContainerProperty("Přiřaď", Button.class, null);
            tabulkaObjednavek.setColumnWidth("Přiřaď", 35);
        
        vlObjednavka.addComponent(tabulkaObjednavek);
        
        HorizontalLayout hlButtons = new HorizontalLayout();
        hlButtons.setSpacing(true);
        hlButtons.setMargin(true);
            Button btnNovaObjednavka = new Button("Nová objednávka");
            btnNovaObjednavka.addListener(new NewObjednavkaAction(this));
        hlButtons.addComponent(btnNovaObjednavka);
        vlObjednavka.addComponent(hlButtons);
        
        addComponent(vlObjednavka);

        int id;
        try {
            id = getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
        } catch (NullPointerException e) {
            id = 1;
        }
        ObjednavkaStateHolder.setState(ObjednavkaStateHolder.VIEW_OBJEDNAVKA_STATE, id, this);
    }
    
    @Override
    public void naplnTabulkuObjednavky() {
        List<Object> objednavky = new ArrayList<Object>();
        objednavky = DaoFactory.getinstance().getObjednavka().getAll(pagingObjComponent.getFrom(), pagingObjComponent.getTo());
        tabulkaObjednavek.removeAllItems();

        for (Object ob : objednavky) {
            com.autosalon.model.Objednavka obj = (com.autosalon.model.Objednavka) ob;
            
            Button btnUkazObjednavku;
            btnUkazObjednavku = new Button("Ukaž");
            btnUkazObjednavku.setStyleName(Button.STYLE_LINK);
            btnUkazObjednavku.addListener(new ShowObjednavkaAction(this));
            btnUkazObjednavku.setData(obj.getId());
            
            Button btnPriradObjednavku;
            btnPriradObjednavku = new Button("Přiřaď");
            btnPriradObjednavku.setStyleName(Button.STYLE_LINK);
            btnPriradObjednavku.addListener(new PriradObjednavkaAction(this));
            btnPriradObjednavku.setData(obj.getId());

            if(obj.getSeller() != null) {
                btnPriradObjednavku.setEnabled(false);
            } 
            
            //zobrazi objednavky prislusici prihlasenemu uzivateli
            if(getSwitcher().getMainWindow().getZakaznik().getRole().getId().floatValue() == Constants.USER_ROLE_CUSTOMER) {
                if (obj.getCustomer().getId().compareTo(getSwitcher().getMainWindow().getZakaznik().getId()) != 0) {    
                    continue;
                }
            }
            tabulkaObjednavek.addItem(new Object[]{obj.getCustomer().getMeno() + " " + obj.getCustomer().getPrimeni(), 
                                                obj.getAutomobil().getModel().getVyrobce().getNazev_vyrobce() + " " + obj.getAutomobil().getModel().getNazev_modelu(),
                                                    obj.getDatum(),
                                                        obj.getSeller() == null ? "" : obj.getSeller().getMeno() + " " + obj.getSeller().getPrimeni(),
                                                            obj.getStav().getStav(),
                                                                btnUkazObjednavku,
                                                                    btnPriradObjednavku}, obj.getAutomobil().getId());
            
        }
        if(getSwitcher().getMainWindow().getZakaznik().getRole().getId().floatValue() == Constants.USER_ROLE_CUSTOMER) {
            tabulkaObjednavek.setVisibleColumns(new Object[]{"ZÁKAZNÍK","AUTOMOBIL","DATUM","PRODEJCE","STAV","Ukaž"});
        }
        
    }
    
    public void initNewObjednavkaForm() {
        newObjednavkaForm = new Form();
        newObjednavkaForm.setCaption("Nová objednávka");
        
        tabulkaAutomobilu = new Table("Automobily");
        tabulkaAutomobilu.setSizeFull();
        tabulkaAutomobilu.setSelectable(true);
        tabulkaAutomobilu.setWidth("100%");
        tabulkaAutomobilu.setHeight("100%");
        tabulkaAutomobilu.addContainerProperty("ID", Long.class, null);
        tabulkaAutomobilu.addContainerProperty("VÝROBCE", String.class, null);
            tabulkaAutomobilu.setColumnWidth("VÝROBCE", 80);
        tabulkaAutomobilu.addContainerProperty("NÁZEV MODELU", String.class, null);
            tabulkaAutomobilu.setColumnWidth("NÁZEV MODELU", 100);
        tabulkaAutomobilu.addContainerProperty("KAROSERIE", String.class, null);
            tabulkaAutomobilu.setColumnWidth("KAROSERIE", 90);    
        tabulkaAutomobilu.addContainerProperty("CENA", Integer.class, null);
            tabulkaAutomobilu.setColumnWidth("CENA", 50);
        tabulkaAutomobilu.addContainerProperty("DATUM VÝROBY", String.class, null);
            tabulkaAutomobilu.setColumnWidth("VÝROBY VYROBY", 80);
        tabulkaAutomobilu.addContainerProperty("NAJETO", Integer.class, null);
            tabulkaAutomobilu.setColumnWidth("NAJETO", 55);
                
        tabulkaAutomobilu.addListener(new ItemClickListener() {

            @Override
            public void itemClick(ItemClickEvent event) {
                Object carId = getTabulkaAutomobilu().getItem(event.getItemId()).getItemProperty("ID");
                try {
                    actuallySelectedCar = (Automobil) DaoFactory.getinstance().getAutomobil().getById(Long.valueOf(carId.toString()));
                    getTfNewObjVyrobce().setValue(actuallySelectedCar.getModel().getVyrobce().getNazev_vyrobce());
                    getTfNewObjModel().setValue(actuallySelectedCar.getModel().getNazev_modelu());
                    getTfNewObjCena().setValue(actuallySelectedCar.getCena());
                    getTfNewObjNajeto().setValue(actuallySelectedCar.getNajeto());
                    getPdNewObjDatum().setValue(actuallySelectedCar.getDatumVyroby());
                } catch (Exception e) {
                    e.printStackTrace(); 
                }
                tsNewObjVybava.removeAllItems();
                for (Object o : DaoFactory.getinstance().getDoplnek().getAll(0, Integer.MAX_VALUE)) {
                    Doplnek doplnek = (Doplnek) o;
                    tsNewObjVybava.addItem(doplnek);
                }
            }
        });
        
        VerticalLayout vlNewObjednavka = new VerticalLayout();
        vlNewObjednavka.setSpacing(true);
        vlNewObjednavka.setMargin(true);
            HorizontalLayout hlTableAndCustom = new HorizontalLayout();
            hlTableAndCustom.setSpacing(true);
                VerticalLayout vlTable = new VerticalLayout();
                vlTable.setSpacing(true);
                    vlTable.addComponent(tabulkaAutomobilu);
                hlTableAndCustom.addComponent(vlTable);
                VerticalLayout vlCustom = new VerticalLayout();
                vlCustom.setSpacing(true);
                vlCustom.setMargin(true);
                    vlCustom.addComponent(new Label("Výrobce"));
                        tfNewObjVyrobce = new TextField();
                        tfNewObjVyrobce.setEnabled(false);
                        vlCustom.addComponent(tfNewObjVyrobce);
                    vlCustom.addComponent(new Label("Model"));
                        tfNewObjModel = new TextField();
                        tfNewObjModel.setEnabled(false);
                        vlCustom.addComponent(tfNewObjModel);
                    vlCustom.addComponent(new Label("Cena"));
                        tfNewObjCena = new TextField();
                        tfNewObjCena.setEnabled(false);
                        vlCustom.addComponent(tfNewObjCena);
                    vlCustom.addComponent(new Label("Pocet najetých kilometrů"));
                        tfNewObjNajeto = new TextField();
                        tfNewObjNajeto.setEnabled(false);
                        vlCustom.addComponent(tfNewObjNajeto);
                    vlCustom.addComponent(new Label("Datum výroby"));
                        pdNewObjDatum = new PopupDateField();
                        pdNewObjDatum.setEnabled(false);
                        pdNewObjDatum.setDateFormat("dd-MM-yyyy");
                        vlCustom.addComponent(pdNewObjDatum);
                    hlTableAndCustom.addComponent(vlCustom);
                VerticalLayout vlVybava = new VerticalLayout();
                vlVybava.setSpacing(true);
                vlVybava.setMargin(true);     
                    tsNewObjVybava = new TwinColSelect("Výbava");
                        tsNewObjVybava.setLeftColumnCaption("Možná výbava");
                        tsNewObjVybava.setRightColumnCaption("Vybraná výbava");
                        tsNewObjVybava.setImmediate(true);
                        tsNewObjVybava.setHeight(235, UNITS_PIXELS);
                        tsNewObjVybava.addListener(new Property.ValueChangeListener() {
                            @Override
                            public void valueChange(ValueChangeEvent event) {
                                Automobil c = (Automobil) DaoFactory.getinstance().getAutomobil().getById(actuallySelectedCar.getId());
                                tsNewObjVybava.setEnabled(false);
                                tsNewObjVybava.setEnabled(true);
                                Collection col = (Collection) tsNewObjVybava.getValue();
                                actuallySelectedCar.setZakladni_vybava(new ArrayList<Doplnek>(col));
                                int cenaDoplnku = 0;
                                for (Doplnek d : actuallySelectedCar.getZakladni_vybava()) {
                                    cenaDoplnku += d.getCena();
                                }
                                tfNewObjCena.setValue(c.getCena()+cenaDoplnku);
                                actuallySelectedCar.setCena((Integer)tfNewObjCena.getValue());
                            }
                        });
                        vlVybava.addComponent(tsNewObjVybava);
               
            vlNewObjednavka.addComponent(hlTableAndCustom);
            vlNewObjednavka.addComponent(vlVybava);
            HorizontalLayout hlTableButtons = new HorizontalLayout();
            hlTableButtons.setSpacing(true);
            hlTableButtons.setMargin(true);
                Button btnVytvoreniObjednavky = new Button("Vytvořit objednávku");
                    btnVytvoreniObjednavky.addListener(new CreateObjednavkaAction(this));
                    hlTableButtons.addComponent(btnVytvoreniObjednavky);
                Button btnBack = new Button("Zpět");
                    btnBack.addListener(new BackObjednavkaAction(this));
                    hlTableButtons.addComponent(btnBack);
                    
            vlNewObjednavka.addComponent(hlTableButtons);
        
        newObjednavkaForm.getLayout().addComponent(vlNewObjednavka);
        
        addComponent(newObjednavkaForm);
        
    }
    
    public void naplnTabulkuAutomobily() {
        ArrayList<Object> automobils = new ArrayList<Object>();
        automobils = (ArrayList<Object>) DaoFactory.getinstance().getAutomobil().getAll(0, Integer.MAX_VALUE);
        tabulkaAutomobilu.removeAllItems();
        for (Object ob : automobils) {
            Automobil auto = (com.autosalon.model.Automobil) ob;
            //vyradi auta, na ktere jiz je vytvorena objednavka
            if (!auto.getObjednavky().isEmpty()) {
                List<com.autosalon.model.Objednavka> objednavky = auto.getObjednavky();
                for (com.autosalon.model.Objednavka o : objednavky) {
                    if (o.getStav().getId().compareTo(Constants.STAV_OBJEDNAVKY_KE_SCHVALENI) == 0 ||
                            o.getStav().getId().compareTo(Constants.STAV_OBJEDNAVKY_SCHVALENO) == 0 ||
                                o.getStav().getId().compareTo(Constants.STAV_OBJEDNAVKY_VYRIZENO) == 0) {
                                        continue;
                    } else {
                        tabulkaAutomobilu.addItem(new Object[]{auto.getId(),
                                                    auto.getModel().getVyrobce().getNazev_vyrobce(),
                                                        auto.getModel().getNazev_modelu(),
                                                            auto.getModel().getKategorie().getTyp_karoserie(),
                                                                auto.getCena(),
                                                                    auto.getDatumVyroby().toString(),
                                                                        auto.getNajeto()}, auto.getId());
                    }
                }
            } else {
                tabulkaAutomobilu.addItem(new Object[]{auto.getId(),
                                                    auto.getModel().getVyrobce().getNazev_vyrobce(),
                                                        auto.getModel().getNazev_modelu(),
                                                            auto.getModel().getKategorie().getTyp_karoserie(),
                                                                auto.getCena(),
                                                                    auto.getDatumVyroby().toString(),
                                                                        auto.getNajeto()}, auto.getId());
            }
            
        }
        tabulkaAutomobilu.setVisibleColumns(new Object[]{"VÝROBCE","NÁZEV MODELU","KAROSERIE","CENA","DATUM VÝROBY","NAJETO"});
    }
    
    public void initCreateObjednavkaForm() {
        createObjednavkaForm = new Form();
        createObjednavkaForm.setCaption("Nová objednávka");
        
        VerticalLayout vlCreateObjednavka = new VerticalLayout();
        vlCreateObjednavka.setSpacing(true);
            hlNewCreateComboZakaznik = new HorizontalLayout();
            hlNewCreateComboZakaznik.setSpacing(true);
            hlNewCreateComboZakaznik.setMargin(true);
                //vyber zakaznika
                Label tmpVyberZakaznika = new Label("Vyber zákazníka:");
                hlNewCreateComboZakaznik.addComponent(tmpVyberZakaznika);
                cbCreateObjZakaznik = new ComboBox();
                cbCreateObjZakaznik.setTextInputAllowed(false);
                cbCreateObjZakaznik.setNullSelectionAllowed(false);
                cbCreateObjZakaznik.setImmediate(true);
                cbCreateObjZakaznik.setWidth(300, UNITS_PIXELS);
                cbCreateObjZakaznik.addListener(new Property.ValueChangeListener() {

                    @Override
                    public void valueChange(ValueChangeEvent event) {
                        String zakaznik = event.getProperty().getValue().toString();
                        Zakaznik z = (Zakaznik) DaoFactory.getinstance().getZakaznik().getOneByParam("email", zakaznik.substring(zakaznik.indexOf(",")+1, zakaznik.lastIndexOf(",")).trim());
                        getTfCreateObjJmeno().setValue(zakaznik.substring(0, zakaznik.indexOf(" ")).trim());
                        getTfCreateObjPrijmeni().setValue(zakaznik.substring(zakaznik.indexOf(" ")+1, zakaznik.indexOf(",")).trim());
                        getCbCreateObjAdresa().removeAllItems();
                        for (Adresa a : z.getAdresy()) {
                            getCbCreateObjAdresa().addItem(a.toString());
                        }
                        getCbCreateObjAdresa().setValue(getCbCreateObjAdresa().getItemIds().iterator().next());
                        getTfCreateObjKontakt().setValue("email: " + zakaznik.substring(zakaznik.indexOf(",")+1, zakaznik.lastIndexOf(",")).trim() +
                                " ,telefon: " + zakaznik.substring(zakaznik.lastIndexOf(",")+1, zakaznik.length()).trim());
                        
                    }
                });
                hlNewCreateComboZakaznik.addComponent(cbCreateObjZakaznik);
        vlCreateObjednavka.addComponent(hlNewCreateComboZakaznik);   
            GridLayout glZakFinAutPoj = new GridLayout(2, 3);
            glZakFinAutPoj.setSpacing(true);
            glZakFinAutPoj.setMargin(true);
                VerticalLayout vlZakaznik = new VerticalLayout();
                vlZakaznik.setSpacing(true);
                vlZakaznik.setMargin(true);
                vlZakaznik.setCaption("Zákazník:");
                    //jmeno a prijmeni
                    HorizontalLayout hlZakaznikJmenoPrijmeni = new HorizontalLayout();
                    hlZakaznikJmenoPrijmeni.setSpacing(true);
                    Label tmpName = new Label("Jméno:");
                    hlZakaznikJmenoPrijmeni.addComponent(tmpName);
                    tfCreateObjJmeno = new TextField();
                    tfCreateObjJmeno.setEnabled(false);
                    hlZakaznikJmenoPrijmeni.addComponent(tfCreateObjJmeno);
                    Label tmpSurname = new Label("Příjmení:");
                    hlZakaznikJmenoPrijmeni.addComponent(tmpSurname);
                    tfCreateObjPrijmeni = new TextField();
                    tfCreateObjPrijmeni.setEnabled(false);
                    hlZakaznikJmenoPrijmeni.addComponent(tfCreateObjPrijmeni);
                vlZakaznik.addComponent(hlZakaznikJmenoPrijmeni);
                    //adresa
                    HorizontalLayout hlZakaznikAdresa = new HorizontalLayout();
                    hlZakaznikAdresa.setSpacing(true);
                    Label tmpAddress = new Label("Adresa:");
                    hlZakaznikAdresa.addComponent(tmpAddress);
                    cbCreateObjAdresa = new ComboBox();
                    cbCreateObjAdresa.setTextInputAllowed(false);
                    cbCreateObjAdresa.setNullSelectionAllowed(false);
                    cbCreateObjAdresa.setWidth(250, UNITS_PIXELS);
                    hlZakaznikAdresa.addComponent(cbCreateObjAdresa);
                vlZakaznik.addComponent(hlZakaznikAdresa);
                    //kontakt
                    HorizontalLayout hlZakaznikKontakt = new HorizontalLayout();
                    hlZakaznikKontakt.setSpacing(true);
                    Label tmpKontakt = new Label("Kontakt:");
                    hlZakaznikKontakt.addComponent(tmpKontakt);
                    tfCreateObjKontakt = new TextField();
                    tfCreateObjKontakt.setEnabled(false);
                    tfCreateObjKontakt.setWidth(350, UNITS_PIXELS);
                    hlZakaznikKontakt.addComponent(tfCreateObjKontakt);
                vlZakaznik.addComponent(hlZakaznikKontakt);
            glZakFinAutPoj.addComponent(vlZakaznik, 0, 0);
                VerticalLayout vlFinancovani = new VerticalLayout();
                vlFinancovani.setSpacing(true);
                vlFinancovani.setMargin(true);
                vlFinancovani.setCaption("Financování:");
                vlFinancovani.setWidth(600, UNITS_PIXELS);
                    //typ financovani
                    HorizontalLayout hlFinancovaniDruh = new HorizontalLayout();
                    hlFinancovaniDruh.setSpacing(true);
                    Label tmpDruhFinancovani = new Label("Typ financování:");
                    hlFinancovaniDruh.addComponent(tmpDruhFinancovani);
                    cbCreateObjFinancovani = new ComboBox();
                    cbCreateObjFinancovani.setNullSelectionAllowed(false);
                    cbCreateObjFinancovani.setTextInputAllowed(false);
                    cbCreateObjFinancovani.setImmediate(true);
                    cbCreateObjFinancovani.setWidth(75, UNITS_PIXELS);
                    cbCreateObjFinancovani.addListener(new Property.ValueChangeListener() {

                        @Override
                        public void valueChange(ValueChangeEvent event) {
                            StavFinancovani sf = (StavFinancovani)event.getProperty().getValue();
                            if (sf.getId() == Constants.STAV_FINANCOVANI_HOTOVOST) {
                                getHlBanka().setVisible(false);
                                getHlFinancovaniFromTo().setVisible(false);
                                getDfCreateObjFinancovaniTo().setValue(new Date(getCal().getTime().getTime()+(7*24*60*60*1000)));
                            } else {
                                getHlBanka().setVisible(true);
                                getHlFinancovaniFromTo().setVisible(true);
                                getDfCreateObjFinancovaniFrom().setValue(new Date(getCal().getTime().getTime()));
                                getDfCreateObjFinancovaniTo().setValue(null);
                            }
                        }
                    });
                    hlFinancovaniDruh.addComponent(cbCreateObjFinancovani);
                vlFinancovani.addComponent(hlFinancovaniDruh);
                    //banka
                    hlBanka = new HorizontalLayout();
                    hlBanka.setSpacing(true);
                    Label tmpBanka = new Label("Banka:");
                    hlBanka.addComponent(tmpBanka);
                    cbCreateObjBanka = new ComboBox();
                    cbCreateObjBanka.setNullSelectionAllowed(false);
                    cbCreateObjBanka.setTextInputAllowed(false);
                    cbCreateObjBanka.setWidth(140, UNITS_PIXELS);
                    hlBanka.addComponent(cbCreateObjBanka);
                    hlBanka.setVisible(false);
                vlFinancovani.addComponent(hlBanka);
                    //datum od-do
                    hlFinancovaniFromTo = new HorizontalLayout();
                    hlFinancovaniFromTo.setSpacing(true);
                    Label tmpFinancovaniFrom = new Label("Od:");
                    hlFinancovaniFromTo.addComponent(tmpFinancovaniFrom);
                    dfCreateObjFinancovaniFrom = new DateField();
                    dfCreateObjFinancovaniFrom.setValue(new Date(getCal().getTime().getTime()));
                    dfCreateObjFinancovaniFrom.setDateFormat("dd-MM-yyyy");
                    dfCreateObjFinancovaniFrom.setWidth(100, UNITS_PIXELS);
                    dfCreateObjFinancovaniFrom.setRequired(true);
                    dfCreateObjFinancovaniFrom.setEnabled(false);
                    hlFinancovaniFromTo.addComponent(dfCreateObjFinancovaniFrom);
                    Label tmpFinancovaniTo = new Label("Do:");
                    hlFinancovaniFromTo.addComponent(tmpFinancovaniTo);
                    dfCreateObjFinancovaniTo = new DateField();
                    dfCreateObjFinancovaniTo.setValue(new Date(getCal().getTime().getTime()+(7*24*60*60*1000)));  //+tyden
                    dfCreateObjFinancovaniTo.setDateFormat("dd-MM-yyyy");
                    dfCreateObjFinancovaniTo.setWidth(100, UNITS_PIXELS);
                    dfCreateObjFinancovaniTo.setRequired(true);
                    hlFinancovaniFromTo.addComponent(dfCreateObjFinancovaniTo);
                    hlFinancovaniFromTo.setVisible(false);
                vlFinancovani.addComponent(hlFinancovaniFromTo);   
            glZakFinAutPoj.addComponent(vlFinancovani, 1, 0);
                VerticalLayout vlAutomobil = new VerticalLayout();
                vlAutomobil.setSpacing(true);
                vlAutomobil.setMargin(true);
                vlAutomobil.setCaption("Automobil:");
                    //vyrobce a model
                    HorizontalLayout hlAutomobilVyrobceModel = new HorizontalLayout();
                    hlAutomobilVyrobceModel.setSpacing(true);
                    Label tmpVyrobce = new Label("Výrobce:");
                    hlAutomobilVyrobceModel.addComponent(tmpVyrobce);
                    tfCreateObjVyrobce = new TextField();
                    tfCreateObjVyrobce.setEnabled(false);
                    hlAutomobilVyrobceModel.addComponent(tfCreateObjVyrobce);
                    Label tmpModel = new Label("Model:");
                    hlAutomobilVyrobceModel.addComponent(tmpModel);
                    tfCreateObjModel = new TextField();
                    tfCreateObjModel.setEnabled(false);
                    hlAutomobilVyrobceModel.addComponent(tfCreateObjModel);
                vlAutomobil.addComponent(hlAutomobilVyrobceModel);
                    //datum vyroby a najeto
                    HorizontalLayout hlAutomobilDatumNajeto = new HorizontalLayout();
                    hlAutomobilDatumNajeto.setSpacing(true);
                    Label tmpDatum = new Label("Datum výroby:");
                    hlAutomobilDatumNajeto.addComponent(tmpDatum);
                    pdCreateObjDatum = new PopupDateField();
                    pdCreateObjDatum.setEnabled(false);
                    hlAutomobilDatumNajeto.addComponent(pdCreateObjDatum);
                    Label tmpNajeto = new Label("Najeto:");
                    hlAutomobilDatumNajeto.addComponent(tmpNajeto);
                    tfCreateObjNajeto = new TextField();
                    tfCreateObjNajeto.setEnabled(false);
                    hlAutomobilDatumNajeto.addComponent(tfCreateObjNajeto);
                vlAutomobil.addComponent(hlAutomobilDatumNajeto);
                    //cena a doplnky
                    HorizontalLayout hlAutomobilCenaDoplnky = new HorizontalLayout();
                    hlAutomobilCenaDoplnky.setSpacing(true);
                    Label tmpCena = new Label("Cena:");
                    hlAutomobilCenaDoplnky.addComponent(tmpCena);
                    tfCreateObjCena = new TextField();
                    tfCreateObjCena.setEnabled(false);
                    hlAutomobilCenaDoplnky.addComponent(tfCreateObjCena);
                    Label tmpDoplnky = new Label("Doplňky:");
                    hlAutomobilCenaDoplnky.addComponent(tmpDoplnky);
                    cbCreateObjDoplnky = new ComboBox();
                    cbCreateObjDoplnky.setWidth(200, UNITS_PIXELS);
                    cbCreateObjDoplnky.setTextInputAllowed(false);
                    hlAutomobilCenaDoplnky.addComponent(cbCreateObjDoplnky);
                vlAutomobil.addComponent(hlAutomobilCenaDoplnky);
            glZakFinAutPoj.addComponent(vlAutomobil, 0, 1);
                VerticalLayout vlPojisteni = new VerticalLayout();
                vlPojisteni.setSpacing(true);
                vlPojisteni.setMargin(true);
                vlPojisteni.setCaption("Pojištění:");
                vlPojisteni.setWidth(600, UNITS_PIXELS);
                    //druh pojistovny
                    HorizontalLayout hlPojistovnaDruh = new HorizontalLayout();
                    hlPojistovnaDruh.setSpacing(true);
                    Label tmpPojistovna = new Label("Pojišťovna:");
                    hlPojistovnaDruh.addComponent(tmpPojistovna);
                    cbCreateObjPojistovna = new ComboBox();
                    cbCreateObjPojistovna.setTextInputAllowed(false);
                    cbCreateObjPojistovna.setNullSelectionAllowed(false);
                    cbCreateObjPojistovna.setImmediate(true);
                    cbCreateObjPojistovna.setWidth(140, UNITS_PIXELS);
                    cbCreateObjPojistovna.addListener(new Property.ValueChangeListener() {

                        @Override
                        public void valueChange(ValueChangeEvent event) {
                            Poistovna p = (Poistovna)event.getProperty().getValue();
                            getLblDescription().setValue(p.getDescription());
                        }
                    });
                    hlPojistovnaDruh.addComponent(cbCreateObjPojistovna);
                vlPojisteni.addComponent(hlPojistovnaDruh);
                    //description
                    HorizontalLayout hlPojistovnaDescription = new HorizontalLayout();
                    hlPojistovnaDescription.setSpacing(true);
                    Label tmpDecription = new Label("Popis:");
                    hlPojistovnaDescription.addComponent(tmpDecription);
                    lblDescription = new Label();
                    lblDescription.setWidth(350, UNITS_PIXELS);
                    hlPojistovnaDescription.addComponent(lblDescription);
                vlPojisteni.addComponent(hlPojistovnaDescription);
                    //datum od-do
                    HorizontalLayout hlPojisteniFromTo = new HorizontalLayout();
                    hlPojisteniFromTo.setSpacing(true);
                    Label tmpPojisteniFrom = new Label("Od:");
                    hlPojisteniFromTo.addComponent(tmpPojisteniFrom);
                    dfCreateObjPojisteniFrom = new DateField();
                    dfCreateObjPojisteniFrom.setValue(new Date(getCal().getTime().getTime()));
                    dfCreateObjPojisteniFrom.setDateFormat("dd-MM-yyyy");
                    dfCreateObjPojisteniFrom.setWidth(100, UNITS_PIXELS);
                    dfCreateObjPojisteniFrom.setRequired(true);
                    dfCreateObjPojisteniFrom.setEnabled(false);
                    hlPojisteniFromTo.addComponent(dfCreateObjPojisteniFrom);
                    Label tmpPojisteniTo = new Label("Do:");
                    hlPojisteniFromTo.addComponent(tmpPojisteniTo);
                    dfCreateObjPojisteniTo = new DateField();
                    dfCreateObjPojisteniTo.setDateFormat("dd-MM-yyyy");
                    dfCreateObjPojisteniTo.setWidth(100, UNITS_PIXELS);
                    dfCreateObjPojisteniTo.setRequired(true);
                    hlPojisteniFromTo.addComponent(dfCreateObjPojisteniTo);                    
                vlPojisteni.addComponent(hlPojisteniFromTo);    
            glZakFinAutPoj.addComponent(vlPojisteni, 1, 1);
        VerticalLayout vlDescription = new VerticalLayout();
                vlDescription.setSpacing(true);
                vlDescription.setMargin(true);
                vlDescription.setCaption("Dodatečné informace:");
                taCreateObjDescription = new TextArea();
                taCreateObjDescription.setWidth(400, UNITS_PIXELS);
                taCreateObjDescription.setHeight(100, UNITS_PIXELS);
                vlDescription.addComponent(taCreateObjDescription);
            glZakFinAutPoj.addComponent(vlDescription, 0, 2);
        vlCreateObjednavka.addComponent(glZakFinAutPoj);
            HorizontalLayout hlCreateObjednavkaButtons = new HorizontalLayout();
            hlCreateObjednavkaButtons.setSpacing(true);
            hlCreateObjednavkaButtons.setMargin(true);
                //buttony
                Button tmpBtnUlozObjednavku = new Button("Ulož objednávku");
                    tmpBtnUlozObjednavku.addListener(new SaveObjednavkaAction(this));
                    hlCreateObjednavkaButtons.addComponent(tmpBtnUlozObjednavku);
                Button tmpBtnZrusObjednavku = new Button("Zruš objednávku");
                    tmpBtnZrusObjednavku.addListener(new CancelObjednavkaAction(this));
                    hlCreateObjednavkaButtons.addComponent(tmpBtnZrusObjednavku);
        vlCreateObjednavka.addComponent(hlCreateObjednavkaButtons);         
       
        createObjednavkaForm.getLayout().addComponent(vlCreateObjednavka);
        
        addComponent(createObjednavkaForm);
    }
    
    public void naplnCreateObjednavkaForm() {
        try {
            //combo box seznam zakazniku
            getCbCreateObjZakaznik().removeAllItems();
            if (getSwitcher().getMainWindow().getZakaznik().getRole().getId().longValue() == Constants.USER_ROLE_SELLER) {
                Role r = (Role) DaoFactory.getinstance().getRole().getById(Constants.USER_ROLE_CUSTOMER);
                List<Object> zakaznici = DaoFactory.getinstance().getZakaznik().getListByParam(Constants.DB_ZAKAZNIK_ID_ROLE, r);       //seznam zakazniku
                for (Object o : zakaznici) {
                    getCbCreateObjZakaznik().addItem(o);
                }
                getCbCreateObjZakaznik().setValue(getCbCreateObjZakaznik().getItemIds().iterator().next());
            } else {
                getCbCreateObjZakaznik().addItem(getSwitcher().getMainWindow().getZakaznik());
                getCbCreateObjZakaznik().setValue(getCbCreateObjZakaznik().getItemIds().iterator().next());
            }
            //vyplneni automobilu
            getTfCreateObjVyrobce().setValue(getActuallySelectedCar().getModel().getVyrobce().getNazev_vyrobce());
            getTfCreateObjModel().setValue(getActuallySelectedCar().getModel().getNazev_modelu());
            getPdCreateObjDatum().setValue(getActuallySelectedCar().getDatumVyroby());
            getTfCreateObjNajeto().setValue(getActuallySelectedCar().getNajeto());
            getTfCreateObjCena().setValue(getTfNewObjCena());
            //vyplni doplnky automobilu
            getCbCreateObjDoplnky().removeAllItems();
            if (!getActuallySelectedCar().getZakladni_vybava().isEmpty()) {
                for (Doplnek d : getActuallySelectedCar().getZakladni_vybava()) {
                    getCbCreateObjDoplnky().addItem(d);
                }
                getCbCreateObjDoplnky().setValue(getCbCreateObjDoplnky().getItemIds().iterator().next());
            }
            //vyplneni financovani
            List<Object> stavyFinancovani = DaoFactory.getinstance().getStavFinancovani().getAll(0, Integer.MAX_VALUE);
            getCbCreateObjFinancovani().removeAllItems();
            for (Object o : stavyFinancovani) {
                getCbCreateObjFinancovani().addItem(o);
            }
            getCbCreateObjFinancovani().setValue(getCbCreateObjFinancovani().getItemIds().iterator().next());
            List<Object> banky = DaoFactory.getinstance().getBanka().getAll(0, Integer.MAX_VALUE);
            getCbCreateObjBanka().removeAllItems();
            for (Object o : banky) {
                Banka b = (Banka) o;
                getCbCreateObjBanka().addItem(b);
            }
            getCbCreateObjBanka().setValue(getCbCreateObjBanka().getItemIds().iterator().next());
            //vyplneni pojisteni
            List<Object> pojistovny = DaoFactory.getinstance().getPoistovna().getAll(0, Integer.MAX_VALUE);
            getCbCreateObjPojistovna().removeAllItems();
            for (Object o : pojistovny) {
                getCbCreateObjPojistovna().addItem(o);
            }
            getCbCreateObjPojistovna().setValue(getCbCreateObjPojistovna().getItemIds().iterator().next());
        } catch (NullPointerException e) {
            getWindow().showNotification("Nastala neočekávaná chyba. Zkuste to prosím později :)");
        }
    }
    
    public void saveObjednavka() {
        try {
            com.autosalon.model.Objednavka objednavkaFinal = new com.autosalon.model.Objednavka();
            //datum
            objednavkaFinal.setDatum(new java.sql.Date(getCal().getTime().getTime()));
            //dodatecne informace
            objednavkaFinal.setDodat_informace(getTaCreateObjDescription().getValue().toString());
            //id_automobilu
            objednavkaFinal.setAutomobil(getActuallySelectedCar());
            //pridane doplnky
            objednavkaFinal.setPridanaVybava(getActuallySelectedCar().getZakladni_vybava());
            //id_zakaznika
            objednavkaFinal.setCustomer((Zakaznik)getCbCreateObjZakaznik().getValue());
            //id_financovani
            Financovani fin = new Financovani();
            //  -- aktualne zaplaceno
                StavFinancovani sfin = (StavFinancovani)getCbCreateObjFinancovani().getValue();
                if (sfin.getId() == Constants.STAV_FINANCOVANI_HOTOVOST) {
                    fin.setAktualneZaplaceno(getActuallySelectedCar().getCena());
                } else if (sfin.getId() == Constants.STAV_FINANCOVANI_PUJCKA){
                    fin.setAktualneZaplaceno(0);
                }
            //  -- datum splatnosti
                if (sfin.getId() == Constants.STAV_FINANCOVANI_HOTOVOST) {
                    fin.setDatumSplatnosti(new Date(objednavkaFinal.getDatum().getTime()+(7*24*60*60*1000)));    //pricte tyden na splatnost
                } else if (sfin.getId() == Constants.STAV_FINANCOVANI_PUJCKA) {
                    java.util.Date splatnost = (java.util.Date)getDfCreateObjFinancovaniTo().getValue();
                    fin.setDatumSplatnosti(new Date(splatnost.getTime()));
                }
            //  -- pocatek financovani
                Date pocatek = (Date)getDfCreateObjFinancovaniFrom().getValue();
                fin.setPocFinancovani(pocatek);
            //  -- id_pujcky
                if (sfin.getId() == Constants.STAV_FINANCOVANI_PUJCKA) {
                    Pujcka puj = new Pujcka();
                    puj.setBanka((Banka)getCbCreateObjBanka().getValue());
                    Date financovaniFrom = (Date)getDfCreateObjFinancovaniFrom().getValue();
                    puj.setPocDatum(financovaniFrom);
                    java.util.Date financovaniTo = (java.util.Date)getDfCreateObjFinancovaniTo().getValue();
                    puj.setKonDatum(new Date(financovaniTo.getTime()));
                    long rozdil = (financovaniTo.getTime() - financovaniFrom.getTime());
                    long pocetmesicu = rozdil /(1000 * 60 * 60 * 24 * 30);
                    puj.setMesicniSplatka(getActuallySelectedCar().getCena() / Long.valueOf(pocetmesicu).intValue());
                    fin.setPujcka(puj);
                    DaoFactory.getinstance().getPujcka().insert(puj);
                }
            //  -- id_stav_financovani
                fin.setStavyFinancovani(sfin);
                DaoFactory.getinstance().getFinancovani().insert(fin);
            objednavkaFinal.setFinancovani(fin);
            //id_pojisteni
            Poisteni poj = new Poisteni();
            //  -- poc_datum
                Date pojisteniFrom = (Date)getDfCreateObjPojisteniFrom().getValue();
                poj.setPocDatum(pojisteniFrom);
            //  -- kon_datum
                java.util.Date pojisteniTo = (java.util.Date)getDfCreateObjPojisteniTo().getValue();
                poj.setKonDatum(new Date(pojisteniTo.getTime()));
            //  -- id_poistovny
                poj.setPoistovna((Poistovna)getCbCreateObjPojistovna().getValue());
                DaoFactory.getinstance().getPoisteni().insert(poj);
            objednavkaFinal.setPoisteni(poj);
            //id_seller
            if (getSwitcher().getMainWindow().getZakaznik().getRole().getId().longValue() == Constants.USER_ROLE_SELLER) {
                objednavkaFinal.setSeller(getSwitcher().getMainWindow().getZakaznik());
            }
            //id_stavu_objednavky
            if (getSwitcher().getMainWindow().getZakaznik().getRole().getId().longValue() == Constants.USER_ROLE_SELLER) {
                objednavkaFinal.setStav((StavObjednavky)DaoFactory.getinstance().getStavObjednavky().getById(Constants.STAV_OBJEDNAVKY_SCHVALENO));
            } else {
                objednavkaFinal.setStav((StavObjednavky)DaoFactory.getinstance().getStavObjednavky().getById(Constants.STAV_OBJEDNAVKY_KE_SCHVALENI));
            }
            
            DaoFactory.getinstance().getObjednavka().insert(objednavkaFinal);
            getWindow().showNotification("Objednávku se podařilo uložit.");
        } catch (Exception e) {
            getWindow().showNotification("Nastala neočekávaná chyba. Zkuste to prosím později :)");
        }
    }
    
    public void initShowObjednavkaForm() {
        showObjednavkaForm = new Form();
        showObjednavkaForm.setCaption("Objednávka");
        
        VerticalLayout vlShowObjednavka = new VerticalLayout();
        vlShowObjednavka.setSpacing(true);
            GridLayout glZakFinAutPoj = new GridLayout(2, 5);
            glZakFinAutPoj.setSpacing(true);
            glZakFinAutPoj.setMargin(true);
                VerticalLayout vlZakaznik = new VerticalLayout();
                vlZakaznik.setSpacing(true);
                vlZakaznik.setMargin(true);
                vlZakaznik.setCaption("Zákazník:");
                vlZakaznik.setWidth(300, UNITS_PIXELS);
                    //jmeno a prijmeni
                    HorizontalLayout hlZakaznikJmenoPrijmeni = new HorizontalLayout();
                    hlZakaznikJmenoPrijmeni.setSpacing(true);
                    Label tmpName = new Label("Jméno:");
                    hlZakaznikJmenoPrijmeni.addComponent(tmpName);
                    lblShowObjJmenoPrijmeni = new Label();
                    hlZakaznikJmenoPrijmeni.addComponent(lblShowObjJmenoPrijmeni);
                vlZakaznik.addComponent(hlZakaznikJmenoPrijmeni);
                    //email
                    HorizontalLayout hlZakaznikEmail = new HorizontalLayout();
                    hlZakaznikEmail.setSpacing(true);
                    Label tmpEmail = new Label("Email:");
                    hlZakaznikEmail.addComponent(tmpEmail);
                    lblShowObjEmail = new Label();
                    hlZakaznikEmail.addComponent(lblShowObjEmail);
                vlZakaznik.addComponent(hlZakaznikEmail);
                    //telefon
                    HorizontalLayout hlZakaznikTelefon = new HorizontalLayout();
                    hlZakaznikTelefon.setSpacing(true);
                    Label tmpTelefon = new Label("Telefon:");
                    hlZakaznikTelefon.addComponent(tmpTelefon);
                    lblShowObjTelefon = new Label();
                    hlZakaznikTelefon.addComponent(lblShowObjTelefon);
                vlZakaznik.addComponent(hlZakaznikTelefon);
            glZakFinAutPoj.addComponent(vlZakaznik, 0, 0);
                VerticalLayout vlFinancovani = new VerticalLayout();
                vlFinancovani.setSpacing(true);
                vlFinancovani.setMargin(true);
                vlFinancovani.setCaption("Financování:");
                    //typ financovani
                    HorizontalLayout hlFinancovaniDruh = new HorizontalLayout();
                    hlFinancovaniDruh.setSpacing(true);
                    Label tmpDruhFinancovani = new Label("Typ financování:");
                    hlFinancovaniDruh.addComponent(tmpDruhFinancovani);
                    lblShowObjFinancovani = new Label();
                    hlFinancovaniDruh.addComponent(lblShowObjFinancovani);
                vlFinancovani.addComponent(hlFinancovaniDruh);
                    //banka
                    HorizontalLayout hlLabelBanka = new HorizontalLayout();
                    hlLabelBanka.setSpacing(true);
                    Label tmpBanka = new Label("Banka:");
                    hlLabelBanka.addComponent(tmpBanka);
                    lblShowObjBanka = new Label();
                    hlLabelBanka.addComponent(lblShowObjBanka);
                vlFinancovani.addComponent(hlLabelBanka);
                    //datum od-do
                    HorizontalLayout hlLabelFinancovaniFromTo = new HorizontalLayout();
                    hlLabelFinancovaniFromTo.setSpacing(true);
                    Label tmpFinancovaniFrom = new Label("Od:");
                    hlLabelFinancovaniFromTo.addComponent(tmpFinancovaniFrom);
                    lblShowObjFinancovaniFrom = new Label();
                    hlLabelFinancovaniFromTo.addComponent(lblShowObjFinancovaniFrom);
                    Label tmpFinancovaniTo = new Label("Do:");
                    hlLabelFinancovaniFromTo.addComponent(tmpFinancovaniTo);
                    lblShowObjFinancovaniTo = new Label();
                    hlLabelFinancovaniFromTo.addComponent(lblShowObjFinancovaniTo);
                vlFinancovani.addComponent(hlLabelFinancovaniFromTo);   
            glZakFinAutPoj.addComponent(vlFinancovani, 1, 0);
                VerticalLayout vlAutomobil = new VerticalLayout();
                vlAutomobil.setSpacing(true);
                vlAutomobil.setMargin(true);
                vlAutomobil.setCaption("Automobil:");
                    //vyrobce a model
                    HorizontalLayout hlAutomobilVyrobceModel = new HorizontalLayout();
                    hlAutomobilVyrobceModel.setSpacing(true);
                    Label tmpVyrobce = new Label("Výrobce:");
                    hlAutomobilVyrobceModel.addComponent(tmpVyrobce);
                    lblShowObjVyrobce = new Label();
                    hlAutomobilVyrobceModel.addComponent(lblShowObjVyrobce);
                    Label tmpModel = new Label("Model:");
                    hlAutomobilVyrobceModel.addComponent(tmpModel);
                    lblShowObjModel = new Label();
                    hlAutomobilVyrobceModel.addComponent(lblShowObjModel);
                vlAutomobil.addComponent(hlAutomobilVyrobceModel);
                    //datum vyroby a najeto
                    HorizontalLayout hlAutomobilDatumNajeto = new HorizontalLayout();
                    hlAutomobilDatumNajeto.setSpacing(true);
                    Label tmpDatum = new Label("Datum výroby:");
                    hlAutomobilDatumNajeto.addComponent(tmpDatum);
                    lblShowObjDatum = new Label();
                    hlAutomobilDatumNajeto.addComponent(lblShowObjDatum);
                    Label tmpNajeto = new Label("Najeto:");
                    hlAutomobilDatumNajeto.addComponent(tmpNajeto);
                    lblShowObjNajeto = new Label();
                    hlAutomobilDatumNajeto.addComponent(lblShowObjNajeto);
                vlAutomobil.addComponent(hlAutomobilDatumNajeto);
                    //cena a doplnky
                    HorizontalLayout hlAutomobilDoplnky = new HorizontalLayout();
                    hlAutomobilDoplnky.setSpacing(true);
                    Label tmpDoplnky = new Label("Doplňky:");
                    hlAutomobilDoplnky.addComponent(tmpDoplnky);
                    lblShowObjDoplnky = new Label();
                    hlAutomobilDoplnky.addComponent(lblShowObjDoplnky);
                vlAutomobil.addComponent(hlAutomobilDoplnky);
            glZakFinAutPoj.addComponent(vlAutomobil, 0, 1);
                VerticalLayout vlPojisteni = new VerticalLayout();
                vlPojisteni.setSpacing(true);
                vlPojisteni.setMargin(true);
                vlPojisteni.setCaption("Pojištění:");
                    //druh pojistovny
                    HorizontalLayout hlPojistovnaDruh = new HorizontalLayout();
                    hlPojistovnaDruh.setSpacing(true);
                    Label tmpPojistovna = new Label("Pojišťovna:");
                    hlPojistovnaDruh.addComponent(tmpPojistovna);
                    lblShowObjPojistovna = new Label();
                    hlPojistovnaDruh.addComponent(lblShowObjPojistovna);
                vlPojisteni.addComponent(hlPojistovnaDruh);
                    //description
                    HorizontalLayout hlPojistovnaDescription = new HorizontalLayout();
                    hlPojistovnaDescription.setSpacing(true);
                    Label tmpDecription = new Label("Popis:");
                    hlPojistovnaDescription.addComponent(tmpDecription);
                    lblShowObjPojistovnaDescription = new Label();
                    //TODO
                    lblShowObjPojistovnaDescription.setWidth(350, UNITS_PIXELS);
                    hlPojistovnaDescription.addComponent(lblShowObjPojistovnaDescription);
                vlPojisteni.addComponent(hlPojistovnaDescription);
                    //datum od-do
                    HorizontalLayout hlPojisteniFromTo = new HorizontalLayout();
                    hlPojisteniFromTo.setSpacing(true);
                    Label tmpPojisteniFrom = new Label("Od:");
                    hlPojisteniFromTo.addComponent(tmpPojisteniFrom);
                    lblShowObjPojisteniFrom = new Label();
                    hlPojisteniFromTo.addComponent(lblShowObjPojisteniFrom);
                    Label tmpPojisteniTo = new Label("Do:");
                    hlPojisteniFromTo.addComponent(tmpPojisteniTo);
                    lblShowObjPojisteniTo = new Label();
                    hlPojisteniFromTo.addComponent(lblShowObjPojisteniTo);                    
                vlPojisteni.addComponent(hlPojisteniFromTo);    
            glZakFinAutPoj.addComponent(vlPojisteni, 1, 1);
        VerticalLayout vlDescription = new VerticalLayout();
                vlDescription.setSpacing(true);
                vlDescription.setMargin(true);
                vlDescription.setCaption("Dodatečné informace:");
                lblShowObjDescription = new Label();
                vlDescription.addComponent(lblShowObjDescription);
            glZakFinAutPoj.addComponent(vlDescription, 0, 2);
        HorizontalLayout hlCena = new HorizontalLayout();
                hlCena.setSpacing(true);
                hlCena.setMargin(true);
                Label tmpCena = new Label("Cena:");
                hlCena.addComponent(tmpCena);
                lblShowObjCena = new Label();
                hlCena.addComponent(lblShowObjCena);
            glZakFinAutPoj.addComponent(hlCena, 0, 3);
        vlShowObjednavka.addComponent(glZakFinAutPoj);
            HorizontalLayout hlShowObjednavkaStav = new HorizontalLayout();
            hlShowObjednavkaStav.setSpacing(true);
            hlShowObjednavkaStav.setMargin(true);
                //stav objednavky combo
                cbShowObjStav = new ComboBox();
                cbShowObjStav.setTextInputAllowed(false);
                cbShowObjStav.setNullSelectionAllowed(false);
                cbShowObjStav.setImmediate(true);
                cbShowObjStav.addListener(new Property.ValueChangeListener() {

                        @Override
                        public void valueChange(ValueChangeEvent event) {
                            //updatuje objednavku pokud zmenime jeji stav
                            try {
                                StavObjednavky so = (StavObjednavky)event.getProperty().getValue();
                                getShowedObjednavka().setStav(so);
                                DaoFactory.getinstance().getObjednavka().update(getShowedObjednavka());
                            } catch (Exception e) {
                                getWindow().showNotification("Nastala neočekávaná chyba. Zkuste to prosím později :)");
                            }
                        }
                    });
                hlShowObjednavkaStav.addComponent(cbShowObjStav);
        vlShowObjednavka.addComponent(hlShowObjednavkaStav);         
            HorizontalLayout hlShowObjednavkaButtons = new HorizontalLayout();
            hlShowObjednavkaButtons.setSpacing(true);
            hlShowObjednavkaButtons.setMargin(true);
                //buttony
                Button tmpBtnZpetObjednavku = new Button("Zpět");
                    tmpBtnZpetObjednavku.addListener(new BackObjednavkaAction(this));
                    hlShowObjednavkaButtons.addComponent(tmpBtnZpetObjednavku);
        vlShowObjednavka.addComponent(hlShowObjednavkaButtons);         
       
        showObjednavkaForm.getLayout().addComponent(vlShowObjednavka);
        
        addComponent(showObjednavkaForm);
        
    }
    
    public void naplnShowObjednavkaForm() {
        try {
            //zakaznik
            getLblShowObjJmenoPrijmeni().setValue(getShowedObjednavka().getCustomer().getMeno() + " " + getShowedObjednavka().getCustomer().getPrimeni());
            getLblShowObjEmail().setValue(getShowedObjednavka().getCustomer().getEmail());
            getLblShowObjTelefon().setValue(getShowedObjednavka().getCustomer().getMobil());
            //automobil
            getLblShowObjVyrobce().setValue(getShowedObjednavka().getAutomobil().getModel().getVyrobce().getNazev_vyrobce());
            getLblShowObjModel().setValue(getShowedObjednavka().getAutomobil().getModel().getNazev_modelu());
            getLblShowObjDatum().setValue(getShowedObjednavka().getAutomobil().getDatumVyroby().toString());
            getLblShowObjNajeto().setValue(String.valueOf(getShowedObjednavka().getAutomobil().getNajeto()) + " km");
            //doplnky
            List<Doplnek> doplnky = getShowedObjednavka().getPridanaVybava();
            int cenaDoplnku = 0;
            if (!doplnky.isEmpty() || doplnky == null) {
                for (Doplnek d : doplnky) {
                    getLblShowObjDoplnky().setValue(getLblShowObjDoplnky().getValue().toString() + ", " + d.getPopis_doplnku());
                    cenaDoplnku += d.getCena();
                }
                //oreze carku na zacatku
                getLblShowObjDoplnky().setValue(getLblShowObjDoplnky().getValue().toString().substring(2, getLblShowObjDoplnky().getValue().toString().length()));
            }
            //cena
            getLblShowObjCena().setValue(String.valueOf(getShowedObjednavka().getAutomobil().getCena()+cenaDoplnku) + " Kč");
            //financovani
            getLblShowObjFinancovani().setValue(getShowedObjednavka().getFinancovani().getStavyFinancovani().getNazevStavu());
            if (getShowedObjednavka().getFinancovani().getPujcka() != null) {
                getLblShowObjBanka().setValue(getShowedObjednavka().getFinancovani().getPujcka().getBanka().getNazevBanky());
            }
            getLblShowObjFinancovaniFrom().setValue(getShowedObjednavka().getFinancovani().getPocFinancovani().toString());
            getLblShowObjFinancovaniTo().setValue(getShowedObjednavka().getFinancovani().getDatumSplatnosti().toString());
            //pojisteni
            getLblShowObjPojistovna().setValue(getShowedObjednavka().getPoisteni().getPoistovna().getNazevPoistovny());
            getLblShowObjPojistovnaDescription().setValue(getShowedObjednavka().getPoisteni().getPoistovna().getDescription());
            getLblShowObjPojisteniFrom().setValue(getShowedObjednavka().getPoisteni().getPocDatum().toString());
            getLblShowObjPojisteniTo().setValue(getShowedObjednavka().getPoisteni().getKonDatum().toString());
            //description
            getLblShowObjDescription().setValue(getShowedObjednavka().getDodat_informace());
            //stav objednavky combo box
            getCbShowObjStav().removeAllItems();
            ArrayList<Object> stavyobjednavek = new ArrayList<Object>();
            stavyobjednavek = (ArrayList<Object>) DaoFactory.getinstance().getStavObjednavky().getAll(0, Integer.MAX_VALUE);
            for (Object ob : stavyobjednavek) {
                StavObjednavky sto = (StavObjednavky)ob;
                getCbShowObjStav().addItem(ob);
                //vybere spravny item v comboboxu
                if (getShowedObjednavka().getStav().getId().compareTo(sto.getId()) == 0) {
                    getCbShowObjStav().setValue(ob);
                }
            }
        } catch (Exception e) {
            getWindow().showNotification("Nastala neočekávaná chyba. Zkuste to prosím později :)");
        }
    }
    
    public ContentSwitcher getSwitcher() {
        return switcher;
    }

    public Calendar getCal() {
        return cal;
    }

    public Automobil getActuallySelectedCar() {
        return actuallySelectedCar;
    }

    public ComboBox getCbCreateObjAdresa() {
        return cbCreateObjAdresa;
    }

    public ComboBox getCbCreateObjBanka() {
        return cbCreateObjBanka;
    }

    public ComboBox getCbCreateObjDoplnky() {
        return cbCreateObjDoplnky;
    }

    public ComboBox getCbCreateObjFinancovani() {
        return cbCreateObjFinancovani;
    }

    public ComboBox getCbCreateObjPojistovna() {
        return cbCreateObjPojistovna;
    }

    public ComboBox getCbCreateObjZakaznik() {
        return cbCreateObjZakaznik;
    }

    public ComboBox getCbShowObjStav() {
        return cbShowObjStav;
    }

    public Form getCreateObjednavkaForm() {
        return createObjednavkaForm;
    }

    public DateField getDfCreateObjFinancovaniFrom() {
        return dfCreateObjFinancovaniFrom;
    }

    public DateField getDfCreateObjFinancovaniTo() {
        return dfCreateObjFinancovaniTo;
    }

    public DateField getDfCreateObjPojisteniFrom() {
        return dfCreateObjPojisteniFrom;
    }

    public DateField getDfCreateObjPojisteniTo() {
        return dfCreateObjPojisteniTo;
    }

    public HorizontalLayout getHlBanka() {
        return hlBanka;
    }

    public HorizontalLayout getHlFinancovaniFromTo() {
        return hlFinancovaniFromTo;
    }

    public HorizontalLayout getHlNewCreateComboZakaznik() {
        return hlNewCreateComboZakaznik;
    }

    public Label getLblDescription() {
        return lblDescription;
    }

    public Label getLblShowObjBanka() {
        return lblShowObjBanka;
    }

    public Label getLblShowObjCena() {
        return lblShowObjCena;
    }

    public Label getLblShowObjDatum() {
        return lblShowObjDatum;
    }

    public Label getLblShowObjDescription() {
        return lblShowObjDescription;
    }

    public Label getLblShowObjDoplnky() {
        return lblShowObjDoplnky;
    }

    public Label getLblShowObjEmail() {
        return lblShowObjEmail;
    }

    public Label getLblShowObjFinancovani() {
        return lblShowObjFinancovani;
    }

    public Label getLblShowObjFinancovaniFrom() {
        return lblShowObjFinancovaniFrom;
    }

    public Label getLblShowObjFinancovaniTo() {
        return lblShowObjFinancovaniTo;
    }

    public Label getLblShowObjJmenoPrijmeni() {
        return lblShowObjJmenoPrijmeni;
    }

    public Label getLblShowObjModel() {
        return lblShowObjModel;
    }

    public Label getLblShowObjNajeto() {
        return lblShowObjNajeto;
    }

    public Label getLblShowObjPojisteniFrom() {
        return lblShowObjPojisteniFrom;
    }

    public Label getLblShowObjPojisteniTo() {
        return lblShowObjPojisteniTo;
    }

    public Label getLblShowObjPojistovna() {
        return lblShowObjPojistovna;
    }

    public Label getLblShowObjPojistovnaDescription() {
        return lblShowObjPojistovnaDescription;
    }

    public Label getLblShowObjTelefon() {
        return lblShowObjTelefon;
    }

    public Label getLblShowObjVyrobce() {
        return lblShowObjVyrobce;
    }

    public Form getNewObjednavkaForm() {
        return newObjednavkaForm;
    }

    public PagingComponent getPagingObjComponent() {
        return pagingObjComponent;
    }

    public PopupDateField getPdCreateObjDatum() {
        return pdCreateObjDatum;
    }

    public PopupDateField getPdNewObjDatum() {
        return pdNewObjDatum;
    }

    public Form getShowObjednavkaForm() {
        return showObjednavkaForm;
    }

    public TextArea getTaCreateObjDescription() {
        return taCreateObjDescription;
    }

    public Table getTabulkaAutomobilu() {
        return tabulkaAutomobilu;
    }

    public Table getTabulkaObjednavek() {
        return tabulkaObjednavek;
    }

    public TextField getTfCreateObjCena() {
        return tfCreateObjCena;
    }

    public TextField getTfCreateObjJmeno() {
        return tfCreateObjJmeno;
    }

    public TextField getTfCreateObjKontakt() {
        return tfCreateObjKontakt;
    }

    public TextField getTfCreateObjModel() {
        return tfCreateObjModel;
    }

    public TextField getTfCreateObjNajeto() {
        return tfCreateObjNajeto;
    }

    public TextField getTfCreateObjPrijmeni() {
        return tfCreateObjPrijmeni;
    }

    public TextField getTfCreateObjVyrobce() {
        return tfCreateObjVyrobce;
    }

    public TextField getTfNewObjCena() {
        return tfNewObjCena;
    }

    public TextField getTfNewObjModel() {
        return tfNewObjModel;
    }

    public TextField getTfNewObjNajeto() {
        return tfNewObjNajeto;
    }

    public TextField getTfNewObjVyrobce() {
        return tfNewObjVyrobce;
    }

    public TwinColSelect getTsNewObjVybava() {
        return tsNewObjVybava;
    }

    public VerticalLayout getVlObjednavka() {
        return vlObjednavka;
    }

    public void setHlNewCreateComboZakaznik(HorizontalLayout hlNewCreateComboZakaznik) {
        this.hlNewCreateComboZakaznik = hlNewCreateComboZakaznik;
    }

    public com.autosalon.model.Objednavka getShowedObjednavka() {
        return showedObjednavka;
    }

    public void setShowedObjednavka(com.autosalon.model.Objednavka showedObjednavka) {
        this.showedObjednavka = showedObjednavka;
    }

    public void setPagingObjComponent(PagingComponent pagingObjComponent) {
        this.pagingObjComponent = pagingObjComponent;
    }
    
    
}